package com.sc.search.controller;

import com.sc.common.pojo.CommonResult;
import com.sc.common.pojo.SearchResult;
import com.sc.search.service.SearchService;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;

/**
 * Created by sc on 2017/10/24.
 */
@Controller
public class searchController {
    @Autowired
    private SearchService searchService;

    @RequestMapping("/q")
    @ResponseBody
    public CommonResult query(@RequestParam(defaultValue = "") String keyword, @RequestParam(value = "page",defaultValue = "1") int page,
                              @RequestParam(value = "rows",defaultValue = "30")int rows){
        try {
            keyword = new String(keyword.getBytes("ISO-8859-1"), "UTF-8");
            SearchResult searchResult = searchService.search(keyword, page, rows);
            return CommonResult.ok(searchResult);
        } catch (SolrServerException e) {
            e.printStackTrace();
            return CommonResult.build(500,"查询出错");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return CommonResult.build(500,"查询出错");
        }
    }
}
